<?php
namespace app\model;
use think\admin\Model;


/**
 * 组局订单
 */
class Orders extends Model
{
    protected $table = "zuju_orders";

    public function merchant()
    {
        return $this->belongsTo(Merchant::class, 'mid');
    }

    public function cassette()
    {
        return $this->belongsTo(Cassette::class, 'cid');
    }

    public function user(){
        return $this->belongsTo(Users::class,'uid');
    }

    public function members(){
        return $this->hasMany(OrderMembers::class,'oid')->order('is_leader desc');
    }

    public function getStartTimeAttr($value){
        return date('Y-m-d H:i',strtotime($value));
    }

    public function getTimerAttr($value, $data)
    {
        $startTime = isset($data['start_timestamp']) ? intval($data['start_timestamp']) : 0;
        $now = time();
        $diff = $startTime - $now;

        if ($diff <= 0) {
            return [
                'day'   => 0,
                'hour'  => 0,
                'minute'=> 0,
                'second'=> 0
            ];
        }

        return [
            'day'    => floor($diff / 86400),           // 计算天数
            'hour'   => floor(($diff % 86400) / 3600),  // 计算小时
            'minute' => floor(($diff % 3600) / 60),     // 计算分钟
            'second' => $diff % 60                      // 计算秒
        ];
    }
}